<?php
/**
 * 
 * Enter description : diều khiển cho từ khóa 
 * @author admin888
 *
 */
class  TukhoaController extends Zend_Controller_Action {

	protected  $_lever = 'null';	
    public function init()
    {
        
       $auth = Zend_Auth::getInstance();
	  if($auth->hasIdentity()){
			$data = $auth->getIdentity();
			$this->view->name = $data->name;
			$this->view->iddangnhap = $data->uid;
			$this->_lever = $data->leve;
			$cd = new Model_Caidat();
			$thongbao = $cd->getthongbao($data->uid);
			$this->view->thongbao = $thongbao;			
			if ( $data->leve != 1 ){
				$this->_redirect('index');
			}			
	   }
       //phần lay out trên
	   $this->view->today =$date = date('m-d-Y H:i:s');
       $reponse = $this->getResponse();
       $reponse->insert('top', $this->view->render('index/top.phtml'));
       $this->view->title = " Quản lý từ khóa - Email Manager ";
    }
	/*
     * admin888 <doan.hut@gmail.com>
     * Description : check nếu chưa login thì chuyển về trang ngoài
     * Note hàm này mọi người có thể dung chung
     */
	public function preDispatch() {
	    $auth = Zend_Auth::getInstance();
	    if (!$auth->hasIdentity()) {
	        if ($this->_request->getActionName() != 'login') {
	            $this->_redirect('login');
	        }
	    }
	}
	// your code 
	public function indexAction()
	{
	 	$tk = new Model_Tukhoa();
	 	$arrtukhoa = $tk->getAlltukhoadanhsach();
	 	$mangthemtukhoa= $tk->mangtukhoakienhe();
	 	$this->view->tukhoa = $arrtukhoa;
	 	$this->view->truongtrondb = $mangthemtukhoa;
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description Bat tat tu khoa
	 */
	public function changsudungtukhoaAction()
	{
		$data = $this->_getAllParams();
		$tk = new Model_Tukhoa();
		$update = $tk->changetukhoasudung($data);
		
		
		$this->getHelper('viewRenderer')->setNoRender(); // set khong su dung template
        $this->_helper->layout->disableLayout(); //disable layout
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description : add tu khoa 
	 */
	public function addtukhoaAction()
	{
		$data = $this->_getAllParams();
		$tk  = new Model_Tukhoa();
		$add = $tk->addthemtukhoa($data);
		if($add){
			$this->_redirect('tukhoa');
		}else{
			$this->_redirect('index');
		}
		$this->getHelper('viewRenderer')->setNoRender(); // set khong su dung template
        $this->_helper->layout->disableLayout(); //disable layout		
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description :sua tua khoa 
	 */
	public function edittukhoaAction()
	{
		$data = $this->_getAllParams();
		$tk = new Model_Tukhoa();
		$update = $tk->updatetukhoa($data);
		if($update){
			$this->_redirect('tukhoa');
		}else{
			$this->_redirect('index');
		}
		$this->getHelper('viewRenderer')->setNoRender(); // set khong su dung template
        $this->_helper->layout->disableLayout(); //disable layout		
	}
	/**
	 * admin888 <pavent@gmail.com>
	 * Enter description : xóa từ khóa
	 */
	public function xoatukhoaAction()
	{
		$tk = new Model_Tukhoa();
		$idtk = $this->_getParam('idtk');
		$xoa = $tk->xoaonetukhoa($idtk);
		$this->getHelper('viewRenderer')->setNoRender(); // set khong su dung template
        $this->_helper->layout->disableLayout(); //disable layout		
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description :xoa all từ khóa
	 */
	public function xoaalltukhoaAction()
	{
		$tk = new Model_Tukhoa();
		$mangtukhoa = $this->_getParam('idtk');
		$tukhoa = explode(',', $mangtukhoa);
		for ($i=0 ; $i < count($tukhoa); $i++){
			$tk->xoaonetukhoa($tukhoa[$i]);
		}
		$this->getHelper('viewRenderer')->setNoRender(); // set khong su dung template
        $this->_helper->layout->disableLayout(); //disable layout			
		 
	}
}